#include "LectorDeNodos.h"


LectorDeNodos::LectorDeNodos() {

}

LectorDeNodos::LectorDeNodos (std::string path, long block_size){
	this->path = path;
	this->block_size=block_size;
	this->manejo_bloques = new ManejoDeBloques(block_size, this->path);
}

Nodo * LectorDeNodos::leer_Nodo(int nroNodo){
	char buffer[block_size];
	char buffer_aux[block_size];
	for (int i = 0 ; i < block_size ; ++i){
		buffer[i] = char(0);
	}
	for (int i = 0 ; i < block_size ; ++i){
		buffer_aux[i] = char(0);
	}

	unsigned int offset = 0;
	this->manejo_bloques->recuperar_bloque_arbol(nroNodo, buffer);
	std::stringstream ss;
	ss << buffer;
	string cadena = ss.str();
	if (cadena.length() > 0){
		this->manejo_bloques->recuperar_bloque_arbol(nroNodo, buffer_aux);
		Interno* aux = new Interno(block_size);
		int nivel = aux->obtenerNivel(buffer_aux);
		delete aux;
		if (nivel == 0){
			Hoja* hoja = new Hoja(block_size);
			hoja->deserializar(buffer, offset);
			return hoja;
		}else{
			Interno* interior = new Interno(block_size);
			interior->deserializar(buffer, offset);
			return interior;

		}
	}

	return NULL;
}

LectorDeNodos::~LectorDeNodos(){
	delete this->manejo_bloques;
}
